
package android.eventfinder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class Map extends MapActivity implements OnGesturePerformedListener{
    /** Called when the activity is first created. */	
	GestureLibrary mlib;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //LinearLayout linearLayout;
    	super.onCreate(savedInstanceState);      
    	setContentView(R.layout.map);

    	GlobalState gs = (GlobalState) getApplication();
    	ArrayList<Venue> venues = gs.getData();
    	MyLocation currentLoc = gs.getLocation();
    	int a = (int) (currentLoc.latitude*1000000);
    	int b = (int) (currentLoc.longitude*1000000);

		MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
        GeoPoint currentLocation = new GeoPoint(a,b);
        mapView.getController().setCenter(currentLocation); // set center of map
        mapView.getController().setZoom(13);
    	
    	if(venues.size() != 0){  		
          //For displaying marker on map
          List<Overlay> mapOverlays = mapView.getOverlays();
      	  Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    	  VenueItermizedOverlay itemizedoverlay = new VenueItermizedOverlay(drawable,this);          
          //for each venue, display it on map
          for(Venue v : venues){
          	int lat = (int) (v.getLocation().latitude * 1000000);
          	int lon = (int) (v.getLocation().longitude *1000000);
              GeoPoint point = new GeoPoint(lat,lon);
              
              String eventInfo = "";
              for(Event e : v.getEventList()){
              	eventInfo = eventInfo + e.getTitle()+"\nArtist: " + e.getArtists() + "\nDate: "+e.getDateString()+"\nTags: " + e.showTags() + ";";
              }
              OverlayItem overlayitem = new OverlayItem(point, v.name + "\n" + v.getStreet(), eventInfo);
              itemizedoverlay.addOverlay(overlayitem);
          }         
          mapOverlays.add(itemizedoverlay);    		
    	}        
               
     }

	
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}


    @Override
    public void onGesturePerformed(GestureOverlayView view, Gesture gesture)
    {
        List<Prediction> predictions = mlib.recognize(gesture);
        for(Prediction predict : predictions)
        {
            if(predict.score > 1)
            { 
                this.finish();
            }
        }
    }
}